package club.weiyuanstudio.java.eedemo.mybatisdemo.dao;

import club.weiyuanstudio.java.eedemo.mybatisdemo.dao.model.Book;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Mapper
@Component
public interface BookMapper {
    @Result(property = "bookName", column = "book_name")
    @Result(property = "publicationDate", column = "publication_date")
    @Select("SELECT * FROM book;")
    List<Book> getAllBook();

    @Result(property = "bookName", column = "book_name")
    @Result(property = "publicationDate", column = "publication_date")
    @Select("SELECT * FROM book WHERE book_name like #{name}")
    List<Book> getBookByName(@Param("name") String name);

    @Result(property = "bookName", column = "book_name")
    @Result(property = "publicationDate", column = "publication_date")
    @Select("SELECT * FROM book WHERE id = #{id}")
    List<Book> getBookById(@Param("id") Long id);

    @Insert("INSERT INTO book (book_name, author, publication_date, price) VALUE (#{bookName}, #{author}, #{publicationDate}, #{price})")
    void addBook(String bookName, String author, Date publicationDate, Double price);

    @Delete("DELETE FROM book WHERE id = #{id}")
    void deleteBookById(@Param("id") Long id);
}
